package eop.graham

import grails.test.*

class StudyServiceTests extends GrailsUnitTestCase {
	protected void setUp() {
		super.setUp()
	}

	protected void tearDown() {
		super.tearDown()
	}

	void testDataSortedBy(){
		mockDomain(Data)
		mockLogging(StudyService, true)
		[
			[1.0, 1.0],
			[2.0, 1.0],
			[1.0, 2.0],
			[1.0, 10.0]
		].each{
			new Data( POmIntensity:it[0], VPIntensity:it[1], geneName:'aa', statMethod:'RMA', probe:'aa', POmFoldChangeP3:1, POmFoldChangeP10:1, VPFoldChangeP3:1, VPFoldChangeP10:1).save(failOnError:true)
		}
		assert Data.count()==4

		StudyService service = [:]
		def l = service.dataSortedBy('POmIntensity', 'VPIntensity')
		assert l.size() == 4
		assert l[0].id == 2
		assert l[3].id == 4
	}

	void testStudySearch(){
		mockDomain(Study)

		[1, 2, 3, 4, 5, 6, 7, 8].each{
			new Study(
					studyName:"Study"+it.toString(),
					geneName:"Testgene"+(it<5?it.toString():"Duplicate"),
					researcher:"Denis",
					studyCriteria:"POmIntensity/POmFoldChangeP10",
					annotation:"Test Annotation "+it.toString(),
					).save(failOnError:true)
		}
		assert Study.count()==8

		StudyService s = [:]
		assert s.studySearch("Testgene1").size() == 1
		assert s.studySearch("TestgeneDuplicate").size() == 4
		assert s.studySearch("").size() == 0
		// want to assert (studyName of bean with geneName=="Testgene2") == "Study2"
		// struggling with syntax !!
		// assert s.studySearch("Testgene2").studyName == "Study2"
	}
}